package com.wangsd.web.sys.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.sys.entity.SysRole;
import com.wangsd.web.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/sys/sysRole")
@Slf4j
public class SysRoleController extends MyController {
    @Resource
    private SysRoleService service;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             SysRole queryObj) {
        log.debug("分页查询列表-controller");
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<SysRole> list = service.selectPage(queryObj);
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "新增或者修改")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody SysRole entity) {
        log.debug("新增或者修改-controller");
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "删除角色")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "角色id", required = true) @PathVariable Long id) {
        log.debug("删除角色-controller");
        service.delete(id);
        return Result.success();
    }

    @ApiOperation(value = "查询所有角色（下拉列表）")
    @GetMapping("/{type}/selectAllRole")
    public Result selectAllRole(@ApiParam(value = "角色类型", required = true) @PathVariable Integer type) {
        log.debug("查询所有角色（下拉列表）-controller");
        List<SysRole> list = service.selectRoleByType(type);
        return Result.success(list);
    }

    @ApiOperation(value = "查询用户拥有的角色")
    @GetMapping("/{userId}/selectRolesByUserId")
    public Object selectRolesByUserId(@ApiParam(value = "用户id", required = true) @PathVariable Long userId) {
        log.debug("查询用户拥有的角色-controller");
        List<SysRole> list = service.selectByUserId(userId);
        List<Long> ids = list.stream().map(SysRole::getId).collect(Collectors.toList());
        return Result.success(ids);
    }
}
